package com.gdut.simple.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public static String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date());
    }

    public static String formatDate(String date) {
        String d = date.substring(0, 10);  // 2017-12-20
        String[] dd = d.split("-");
        if (dd.length != 3) {
            throw new IllegalArgumentException("unexcept format");
        }
        int year = Integer.valueOf(dd[0]);
        int month = Integer.valueOf(dd[1]);
        if (month < 0 || month > 12) {
            throw new IllegalArgumentException("unexcept month format");
        }
        int day = Integer.valueOf(dd[2]);
        if (day < 0 || day > 31) {
            throw new IllegalArgumentException("unexcept day format");
        }
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        if (year == currentYear) {
            if (month == currentMonth) {
                if (day == currentDay) {
                    return date.substring(10, date.length());
                }
                return getDiffDay(day, currentDay);
            }
            return getDiffMonth(month, currentMonth, day, currentDay);
        } else {
            return d;
        }
    }

    private static String getDiffDay(int sourceDay, int currentDay) {
        int diff = Math.abs(currentDay - sourceDay);
        switch (diff) {
            case 1:
                return "一天前";
            case 2:
                return "两天前";
            case 3:
                return "三天前";
            case 4:
                return "四天前";
            case 5:
                return "五天前";
            case 6:
                return "六天前";
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
                return "一周前";
            default:
                return "半月前";
        }
    }

    private static String getDiffMonth(int sourceMonth, int currentMonth, int sourceDay, int currentDay) {
        int diff = Math.abs(currentMonth - sourceMonth);
        switch (diff) {
            case 1:  // 3.29-4.9
                if (currentDay + getDayOfMonth(sourceMonth) - sourceDay > getDayOfMonth(sourceMonth)) {
                    return "一月前";
                } else {
                    return getDiffDay(sourceDay, currentDay + getDayOfMonth(sourceMonth));
                }
            case 2:
                return "两月前";
            case 3:
                return "三月前";
            case 4:
                return "四月前";
            case 5:
                return "五月前";
            default:
                return "半年前";
        }
    }

    private static int getDayOfMonth(int month) {
        switch (month) {
            case 1:
                return 31;
            case 2:
                return 28;
            case 3:
                return 31;
            case 4:
                return 30;
            case 5:
                return 31;
            case 6:
                return 30;
            case 7:
                return 31;
            case 8:
                return 31;
            case 9:
                return 30;
            case 10:
                return 31;
            case 11:
                return 30;
            case 12:
                return 31;
            default:
                return 0;
        }
    }

}
